package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UserInsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserinfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController注解：
 *   1.组合注解,等价于:Controller + ResponseBody 注解;
 *   2.加完此注解后,所有的方法上都不需要再添加 ResponseBody 注解;
 */

//@Controller
@Validated
@Api(tags = "01.用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    //自动装配
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.添加用户功能
     */
    @ApiOperation("添加用户功能")
    @PostMapping("insert")
    public JsonResult insert(@Validated UserInsertDTO userInsertDTO){
        /*
            梳理业务逻辑,确定接口;
            1.接收客户端传递的数据[用户名、密码、昵称];
            2.数据合法性校验;
            3.确定用户名是否被占用[SELECT接口];
              3.1 被占用,返回提示信息;
              3.2 未被占用,插入数据[INSERT接口],返回提示信息;
         */
        log.debug("userInserDTO = " + userInsertDTO);
        User user = new User();
        //复制属性
        BeanUtils.copyProperties(userInsertDTO, user);
        user.setCreated(new Date());

        log.debug("user = " + user);

        userMapper.insertUser(user);

        return JsonResult.ok();
    }

    /**
     * 2.查询用户列表功能
     */
    @ApiOperation("查询用户列表功能")
    @GetMapping("userList")
    public JsonResult userList(){
        List<UserListVO> userListVOS = userMapper.selectUserList();
        log.debug("userListVOS = " + userListVOS);

        return JsonResult.ok(userListVOS);
    }

    /**
     * 3.删除用户功能
     */
    @ApiImplicitParam(name = "id", value = "用户编号", required = true, dataType = "int")
    @ApiOperation("删除用户功能")
    @GetMapping("delete")
    public JsonResult delete(@Range(min = 1, max = 100, message = "用户编号必须在1-100之间") @RequestParam int id){
        log.debug("id = " + id);
        userMapper.deleteUserById(id);

        return JsonResult.ok();
    }

    /**
     * 4.修改用户功能
     */
    @PostMapping("update")
    public JsonResult update(User user){
        log.debug("user = " + user);
        userMapper.updateUserById(user);

        return JsonResult.ok();
    }

    /**
     * 5.查询指定用户的用户名和昵称
     *   http://localhost:8080/v1/users/用户ID
     */
    @GetMapping("{id}")
    public JsonResult getUserinfo(@PathVariable int id){
        log.debug("id = " + id);
        UserinfoVO userinfoVO = userMapper.selectUserinfoById(id);

        return JsonResult.ok(userinfoVO);
    }
}









